package com.treeengine.game.audio;

import java.io.IOException;


import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;

public class Audio implements IAudio{

	AssetManager assets;
	SoundPool soundPool;
	
	public Audio(Activity activity){
		activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		assets = activity.getAssets();
		soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
	}
	
	@Override
	public IMusic newMusic(String filename) {
		try{
			AssetFileDescriptor assetDescriptor = assets.openFd(filename);
			return new Music(assetDescriptor);
		}catch(IOException e){
			return null;			
		}
	}

	@Override
	public ISound newSound(String filename) {
		try {
			AssetFileDescriptor assetDescriptor = assets.openFd(filename);
			int soundId = soundPool.load(assetDescriptor, 0);
			return new Sound(soundPool, soundId);
		} catch (IOException e) {
			return null;
		}
	}

}
